// pages/orderCenter/orderCenter.js
import {
  OrderModel
} from '../../../model/order'
var order = new OrderModel()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    picked: 0,
    listArr: [],
    isAgent: 0,
    current: 1,
    size: 10,
    totalCount: 0
  },
  orderDetail(e){
    var data = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/customOrder/detail/detail?id='+data.id,
    })
  },
  toAlbum(e){
    var data = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '/pages/album/album?id='+data.id+"&name="+data.name+"&code="+data.orderCode,
    })
  },
  toDiary(e){
    var data = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '/pages/diary/list/list?id='+data.id+"&name="+data.name+"&code="+data.orderCode,
    })
  },
  // 点击全部
  all() {
    this.setData({
      picked: this.picked = 0,
      listArr: [],
      current: 1
    })
    order.customMadeOrderList('', '', 0).then(res => {
      this.setData({
        listArr: res.data.records,
        totalCount: res.data.totalCount
      })
    })
  },
  // 点击待付款
  toBePay() {
    this.setData({
      picked: this.picked = 1,
      listArr: [],
      current: 1
    })
    order.customMadeOrderList('', '', 1).then(res => {
      this.setData({
        listArr: res.data.records,
        totalCount: res.data.totalCount
      })
    })
  },
  //点击待发货
  toBeDelivered() {
    this.setData({
      picked: this.picked = 2,
      listArr: [],
      current: 1
    })
    order.customMadeOrderList('', '', 2).then(res => {
      this.setData({
        listArr: res.data.records,
        totalCount: res.data.totalCount
      })
    })
  },
  // 点击待收货
  toBeReceived() {
    this.setData({
      picked: 3,
      listArr: [],
      current: 1
    })
    order.customMadeOrderList('', '', 3).then(res => {
      this.setData({
        listArr: res.data.records,
        totalCount: res.data.totalCount
      })
    })
  },
  // 点击售后
  afterSale() {
    this.setData({
      picked: this.picked = 4,
      listArr: [],
      current: 1
    })
    order.customMadeOrderList('', '', 3).then(res => {
      this.setData({
        listArr: res.data.records
      })
    })
  },
  // 滑动到底部
  scrollToBottom() {
    if (this.data.listArr.length < this.data.totalCount) {
      this.setData({
        current: this.data.current = this.data.current + 1
      })
      order.orderList(this.data.current, this.data.size, this.data.picked).then(res => {
        this.setData({
          listArr: this.data.listArr = this.data.listArr.concat(res.data.list)
        })
        // console.log(this.data.current)
        // console.log( this.data.listArr.push.apply(this.data.listArr,res.data.list))
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    order.customMadeOrderList('','',this.data.picked).then(res => {
      this.setData({
        listArr: res.data.records,
        totalCount: res.data.totalCount
      });
      // console.log(this.data.current)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isAgent: app.globalData.userInfo.isAgent
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})